<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-12-14 22:15:01
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-21 21:12:40
-->
<template>
  <div>
    <el-row>
      <el-col :span="6" style="padding-right:.5em;border-right:2px solid #f4f4f4">
        <el-tree :data="eds" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>
      <el-col :span="18">
        <div v-if="params.device_id"> 
          {{params}} 
          <div class="engineer">
            <h3>工程信息</h3>
            <el-row>
              <el-col :span="12">
                <span class="label">工程名称</span>
                <span class="value">{{device.engineer.name}}</span>
              </el-col>
              <el-col :span="12">
                <span class="label">工程编号</span>
                <span class="value">{{device.engineer.serial_number}}</span>
              </el-col>
            </el-row>
          </div>
          <div class="device">
            <h3>设备信息</h3>
            <el-row>
              <el-col :span="12">
                <span class="label">设备名称</span>
                <span class="value">{{device.name}}</span>
              </el-col>
              <el-col :span="12">
                <span class="label">设备名称</span>
                <span class="value">{{device.serial_number}}</span>
              </el-col>
            </el-row>
          </div>
          
        </div>
        <div v-else style="line-height:3em;color:red;text-align:center">请选择设备</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {get} from '@/utils/request'
export default {
  // 变量
  data(){
    return {
      defaultProps:{
        children: 'children',
        label: 'name'
      },
      eds:[],
      params:{},
      device:{}
    }
  },
  // 方法
  methods:{
    loadDeviceDetails(){
      let url = "/device/findDeviceDetail";
      get(url,{id:this.params.device_id}).then(resp => {
        this.device = resp.data;
      });
    },
    handleNodeClick(node){
      if(node.engineer_id ){
        // 点击了设备
        this.$set(this.params,'engineer_id',node.engineer_id)
        this.$set(this.params,'device_id',node.id)

        // 请求设备信息
        this.loadDeviceDetails();
      }
    },
    // 加载工程设备树
    loadEngineerDevices(){
      let url = "/engineer/findEngineerDeviceTree"
      get(url).then(resp => {
        this.eds = resp.data;
      })
    }
  },
  // 页面加载完毕后执行
  mounted() {
    this.loadEngineerDevices();
  },
}
</script>